package com.avic.modules.equipment.rest;

import com.avic.dozer.service.IGenerator;
import com.avic.modules.equipment.domain.Equipment;
import com.avic.modules.equipment.domain.Station;
import com.avic.modules.equipment.service.EquipmentService;
import com.avic.modules.equipment.service.dto.EquipmentDto;
import com.avic.modules.equipment.service.dto.EquipmentQueryCriteria;
import com.avic.modules.equipment.service.mapper.EquipmentMapper;
import com.avic.modules.log.domain.vo.LogEnumeration;
import com.avic.modules.log.domain.vo.LogVo;
import com.avic.modules.log.service.LogsService;
import com.avic.modules.system.domain.User;
import com.avic.modules.system.domain.UsersRoles;
import com.avic.modules.system.service.dto.DeptDto;
import com.avic.modules.system.service.mapper.SysUserMapper;
import com.avic.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author zhangzikang
 * @date 2022-08-14
 */
@Api(tags = "设备管理: 设备接口")
@RestController
@RequestMapping("/api/equipment")
@AllArgsConstructor
public class EquipmentController {

    private final EquipmentService equipmentService;
    private final IGenerator generator;
    private final LogsService logsService;
    private final EquipmentMapper equipmentMapper;
    private final SysUserMapper sysUserMapper;

    /**
     * 查看设备
     * @param criteria
     * @return
     */
    @ApiOperation("查看设备信息")
    @GetMapping
    @PreAuthorize("@el.check('admin','equipment:list')")
    public ResponseEntity<Object> getEquipment(EquipmentQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(equipmentService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    /**
     * 查看设备信息
     * @param criteria
     * @return
     */
    @ApiOperation("查看设备信息")
    @GetMapping("/all")
    public ResponseEntity<Object> getEquipmentAll(EquipmentQueryCriteria criteria){
        List<Equipment> equipment = equipmentService.queryAll(criteria);
        List<EquipmentDto> convert = generator.convert(equipment, EquipmentDto.class);
        for(EquipmentDto equip:convert){
            User user = sysUserMapper.selectById(equip.getPrincipalId());
            User user2 = sysUserMapper.selectById(equip.getMaintainPersonId());
            if(user2!=null){
                equip.setMaintainPersonName(user2.getIsDel()==0?user2.getNickName():null);
            }
            if(user!=null){
                equip.setPrincipalName(user.getIsDel()==0?user.getNickName():null);
            }
        }
        return new ResponseEntity<>(convert, HttpStatus.OK);
    }

    /**
     * 查看设备信息
     * @param criteria
     * @return
     */
    @ApiOperation("查看设备信息")
    @GetMapping("/station")
    public ResponseEntity<Object> getEquipmentStation(EquipmentQueryCriteria criteria){
        List<Equipment> equipment = equipmentService.queryAll(criteria);
        List<String> stations = new ArrayList<>();
        for(Equipment equip:equipment){
            if(equip.getStackNumber()!=null && !equip.getStackNumber().equals(""))
            if(!stations.contains(equip.getStackNumber())){
                stations.add(equip.getStackNumber());
            }
        }
        return new ResponseEntity<>(stations, HttpStatus.OK);
    }

    /**
     * 导出设备信息
     * @param response
     * @param criteria
     */
    @ApiOperation("导出设备信息")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','equipment:download')")
    public void download(HttpServletResponse response, EquipmentQueryCriteria criteria,Pageable pageable) throws IOException {

        equipmentService.download(generator.convert(equipmentService.queryAlls(criteria,pageable), EquipmentDto.class), response);
    }

    /**
     * 添加设备信息
     * @param equipment
     * @return
     */
    @ApiOperation("添加设备信息")
    @PostMapping
    @PreAuthorize("@el.check('admin','equipment:add')")
    public ResponseEntity<Object> addEquipment(@Validated @RequestBody Equipment equipment){
        equipmentService.addEquipment(equipment);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 修改设备信息
     * @param equipment
     * @return
     */
    @ApiOperation("修改设备信息")
    @PutMapping
    @PreAuthorize("@el.check('admin','equipment:edit')")
    public ResponseEntity<Object> editEquipment(@Validated @RequestBody Equipment equipment){
        equipmentService.editEquipment(equipment);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除设备信息
     * @param ids
     * @return
     */
    @ApiOperation("删除设备信息")
    @DeleteMapping
    @PreAuthorize("@el.check('admin','equipment:del')")
    public ResponseEntity<Object> delEquipment(@RequestBody Set<String> ids){
        List<String> strings = new ArrayList<>();
        for (String id : ids) {
            Equipment equip = equipmentMapper.selectById(id);
            strings.add(equip.getEquipSonName());
        }
        // 创建操作日志
        LogVo logVo = new LogVo();
        String name = "[删除]了"+ " [" + strings.toString() + "] " +"设备台账信息";
        logVo.setBehavior(name);
        logVo.setAction(LogEnumeration.Delete.getMessage());
        logsService.save(logVo);

        equipmentService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 查看设备
     * @param criteria
     * @return
     */
    @ApiOperation("查看设备信息")
    @GetMapping("/see")
    public ResponseEntity<Object> getSeeEquipment(EquipmentQueryCriteria criteria){
        criteria.setDeviceType(1);
        List<Equipment> equipment = equipmentService.queryAll(criteria);
        List<Equipment> equipment1 = equipmentService.seeEquip(equipment);
        return new ResponseEntity<>(equipment1,HttpStatus.OK);
    }

    @ApiOperation("导出模板数据")
    @GetMapping(value = "/template")
    public void addImport(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for(int i=0;i<10;i++){
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("序号", "");
            map.put("设备编号", "");
            map.put("资产编码", "");
            map.put("设备名称", "");
            map.put("资产状态", "");
            map.put("设备类型", "");
            map.put("状态说明", "");
            map.put("型号规格", "");
            map.put("所属车间", "");
            map.put("生产厂家", "");
            map.put("设备位置", "");
            map.put("供应商", "");
            map.put("负责人", "");
            map.put("购买时间", "");
            map.put("联系方式", "");
            map.put("启用时间", "");
            map.put("维护班组", "");
            map.put("站位", "");
            map.put("维护人员", "");
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @ApiOperation("导入用户数据")
    @PostMapping(value = "/import")
    @PreAuthorize("@el.check('admin','equipment:import')")
    public ResponseEntity<Object> addImport(@RequestParam(value = "file") MultipartFile file){
        equipmentService.equipImport(file);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
